
package br.com.easyrhour3x.login;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import br.com.easyrhour3x.controller.Autenticacao;
import br.com.easyrhour3x.main.ConfigUIPadrao;
import br.com.easyrhour3x.main.UIMain;
import br.com.easyrhour3x.util.Teclas;
import de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel;

public class UILogin extends ConfigUIPadrao
{

   private static final long serialVersionUID = 1L;
   private JPanel contentPane;
   private JTextField textLogin;
   private JPasswordField textSenha;
   private String login, senha;

   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
      {

         public void run()
         {
            try
            {
               UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
               UILogin frame = new UILogin();
               frame.setVisible(true);
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      });
   }

   @Override
   public void sair()
   {
      System.exit(0);
   }

   private void carregarTelaPrincipal()
   {
      new UIMain().setVisible(true);
      dispose();
   }

   @SuppressWarnings("deprecation")
   private void logar()
   {
      login = textLogin.getText().trim();
      senha = textSenha.getText().trim();
      Autenticacao autenticacao = new Autenticacao();
      if (login.equals("") || senha.equals(""))
      {
         JOptionPane.showMessageDialog(this, "Preencha o login e a senha!");
      }
      else if (autenticacao.validarLogin(login, senha))
      {
         carregarTelaPrincipal();
      }
      else
      {
         JOptionPane.showMessageDialog(this, "Login ou senha incorreto!");
      }
   }

   public UILogin()
   {
      setAlwaysOnTop(true);
      addWindowListener(new WindowAdapter()
      {

         @Override
         public void windowClosing(WindowEvent e)
         {
            sair();
         }
      });
      setResizable(false);
      setTitle("Easy RHour 3x");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(292, 154);
      setLocationRelativeTo(null);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);

      textLogin = new JTextField();
      textLogin.setFont(new Font("Tahoma", Font.PLAIN, 14));
      textLogin.setColumns(10);

      JLabel lblLogin = new JLabel("Login:");
      lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 14));

      JLabel lblSenha = new JLabel("Senha:");
      lblSenha.setFont(new Font("Tahoma", Font.PLAIN, 14));

      JButton btnNewButton = new JButton("Logar");
      btnNewButton.addActionListener(new ActionListener()
      {

         public void actionPerformed(ActionEvent arg0)
         {
            logar();
         }
      });
      btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));

      textSenha = new JPasswordField();
      textSenha.addKeyListener(new KeyAdapter()
      {

         @Override
         public void keyPressed(KeyEvent e)
         {
            if (e.getKeyCode() == Teclas.ENTER.getValorTecla())
            {
               logar();
            }
         }
      });

      textSenha.setFont(new Font("Tahoma", Font.PLAIN, 14));
      GroupLayout gl_contentPane = new GroupLayout(contentPane);
      gl_contentPane
               .setHorizontalGroup(gl_contentPane
                        .createParallelGroup(Alignment.LEADING)
                        .addGroup(
                           Alignment.TRAILING,
                           gl_contentPane
                                    .createSequentialGroup()
                                    .addContainerGap()
                                    .addGroup(
                                       gl_contentPane
                                                .createParallelGroup(
                                                   Alignment.TRAILING)
                                                .addComponent(btnNewButton,
                                                   Alignment.LEADING,
                                                   GroupLayout.DEFAULT_SIZE, 256,
                                                   Short.MAX_VALUE)
                                                .addGroup(
                                                   Alignment.LEADING,
                                                   gl_contentPane
                                                            .createSequentialGroup()
                                                            .addComponent(lblSenha)
                                                            .addPreferredGap(
                                                               ComponentPlacement.RELATED)
                                                            .addComponent(
                                                               textSenha,
                                                               GroupLayout.DEFAULT_SIZE,
                                                               209,
                                                               Short.MAX_VALUE))
                                                .addGroup(
                                                   Alignment.LEADING,
                                                   gl_contentPane
                                                            .createSequentialGroup()
                                                            .addGap(6)
                                                            .addComponent(lblLogin)
                                                            .addPreferredGap(
                                                               ComponentPlacement.RELATED)
                                                            .addComponent(
                                                               textLogin,
                                                               GroupLayout.DEFAULT_SIZE,
                                                               208,
                                                               Short.MAX_VALUE)))
                                    .addContainerGap()));
      gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
               .addGroup(
                  gl_contentPane
                           .createSequentialGroup()
                           .addContainerGap()
                           .addGroup(
                              gl_contentPane
                                       .createParallelGroup(Alignment.BASELINE)
                                       .addComponent(textLogin,
                                          GroupLayout.PREFERRED_SIZE,
                                          GroupLayout.DEFAULT_SIZE,
                                          GroupLayout.PREFERRED_SIZE)
                                       .addComponent(lblLogin))
                           .addPreferredGap(ComponentPlacement.RELATED)
                           .addGroup(
                              gl_contentPane
                                       .createParallelGroup(Alignment.BASELINE)
                                       .addComponent(textSenha,
                                          GroupLayout.PREFERRED_SIZE,
                                          GroupLayout.DEFAULT_SIZE,
                                          GroupLayout.PREFERRED_SIZE)
                                       .addComponent(lblSenha))
                           .addGap(15)
                           .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 33,
                              GroupLayout.PREFERRED_SIZE)
                           .addContainerGap(30, Short.MAX_VALUE)));
      contentPane.setLayout(gl_contentPane);
   }
}
